package com.mazong.serversecbasic.service.impl;

import com.mazong.serversecbasic.mapper.IDbSecurityMapper;
import com.mazong.serversecbasic.pojo.TbRole;
import com.mazong.serversecbasic.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    IDbSecurityMapper iDbSecurityMapper;

    /**
     * TODO loadUserByUsername,根据用户的名字获取用户信息
     * @param s
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //查数据库
        TbUser user = iDbSecurityMapper.loadUserByUsername( s );
        if (null != user) {
            List<TbRole> roles = iDbSecurityMapper.getRolesByUserId( user.getId() );
            user.setAuthorities( roles );
        }



        return user;
    }
}
